/*************************************************************
Date: 2010-12
Description: 编码解码接口定义
***********************************************************/

#ifndef __CODER_DEFINE_H__
#define __CODER_DEFINE_H__
#include "common_define.h"

namespace ssdasn {
/*
* 获得当前api版本号
*/
int get_api_version();
/**
 * trmem_encode - encode an request to asn packet
 * Input:
 * @op:		operation code
 * @seq:	request id
 * @key:	request key
 * @data:   request data
 * @buffer: use to build asn packet
 * @buffer_len: buffer length
 * @cas: compare and swap
 * @expire: time out
 * @rsp_ext: extend request
 * @req_ext:passwd, col
 * Output:
 * @pkt_ptr: asn packet buffer pointer
 * @pkt_len: asn packet buffer length
 
 * Return:
 * 0 means success, otherwise means failed.
 */
int trmem_encode(int bid, int op,int seq,string &key,string &data,
 		   char *buffer, int buffer_len,
 		   char *&pkt_ptr, int &pkt_len, 
 		   int cas = -1, int expire = 0,
 		   int offset = 0, int len = -1, TReqExt *req_ext = NULL);

/**
 * decode - decode asn packet
 * Input:
 * @buffer: 包的起始地址
 * @buffer_len: 包的实际长度，根据trmem_pkt_complete的返回值
 
 * Output:
 * @bid		busyness identifier
 * @op:		operation code
 * @seq:	request id
 * @key:	request key
 * @data:   request data
 * @retcode: ssd server return code
 * @cas: compare and swap
 * @rsp_ext: extend response
 * Return:
 * 0 means success, otherwise means failed.
 */
int trmem_decode(char *buffer, int buffer_len,
		   int &bid, int &op,int &seq,string &key,
		   string &data,int &retcode, int&cas, TRspExt *rsp_ext = NULL);






/**
 * trmem_pkt_complete - asn packet is completely received?
 * Input:
 * @data: packet pointer
 * @data_len: packet length

 * Return:
 * >0 包完整，并返回包的实际长度
 * =0 包不完整，需要后续的数据
 * <0 出错，包不合法
 */
int trmem_pkt_complete(char* data,int data_len);


/**
 * ssd_encode - encode an request to asn packet
 * Input:
 * @op:		operation code
 * @seq:	request id
 * @v_node:	request key/data node vector
 * @buffer: use to build asn packet
 * @buffer_len: buffer length
 * @req_ext:passwd
 * Output:
 * @pkt_ptr: asn packet buffer pointer
 * @pkt_len: asn packet buffer length

 * Return:
 * 0 means success, otherwise means failed.
 */
int trmem_encode_list(int bid, int op,int seq,vector<TKeyNode> &v_node,
 		   char *buffer, int buffer_len,
 		   char *&pkt_ptr, int &pkt_len, TReqExt *req_ext = NULL);

int trmem_encode_list_v2_internal(int bid, int op,int seq,vector<TKeyNodeInternal> &v_node,
 		   char *buffer, int buffer_len,
 		   char *&pkt_ptr, int &pkt_len, TReqExt *req_ext = NULL);

/**
 * decode - decode asn packet
 * Input:
 * @buffer: 包的起始地址
 * @buffer_len: 包的实际长度，根据trmem_pkt_complete的返回值

 * Output:
 * @op:		operation code
 * @seq:	request id
 * @v_node:	request data vector

 * Return:
 * 0 means success, otherwise means failed.
 */
int trmem_decode_list(char *buffer, int buffer_len,
		   int &op,int &seq, vector<TKeyNode> &v_node);
int trmem_decode_list_v2_internal(char *buffer, int buffer_len,
		   int &op,int &seq, vector<TKeyNodeInternal> &v_node);

/**
 * decode - decode asn packet header
 * Input:
 * @buffer: 包的起始地址
 * @buffer_len: 包的实际长度，根据trmem_pkt_complete的返回值

 * Output:
 * @op:		operation code
 * @seq:	request id

 * Return:
 * 0 means success, otherwise means failed.
 */

int trmem_decode_head(char *buffer, int buffer_len, int &op,int &seq);


}
#endif

